import Vue from 'vue'
import Vuex from 'vuex'
import vuexPersist from "vuex-persist"
Vue.use(Vuex)

import { Toast } from 'vant';
export default new Vuex.Store({
  state: {
    shopadd:[],
    likeadd:[],
    user:"",
  },
  mutations: {
    add(state,item){
      var flag=false
      state.shopadd.forEach((res)=>{
        if(res.product_id==item.product_id){
          res.num++
          flag=ture
        }
      })
      if(flag==false){
        Vue.set(item,"num",1)
        state.shopadd.push(item)
        Toast('已添加至购物车');
      }
    },
    remove(state,item){
      state.shopadd.forEach((res,index)=>{
        if(res.product_id==item.product_id){
          state.shopadd.splice(index,1)
        }
      })
    },
    changenum(){},
    like(state,item){
      var flag=false
      state.likeadd.forEach((res)=>{
        if(res.product_id==item.product_id){
          flag=ture
          Toast('不能重复添加');
        }
      })
      if(flag==false){
        Toast('已添加至收藏');
        state.likeadd.push(item)
      }
    },
    out(state,item){
      state.likeadd.forEach((res,index)=>{
        if(res.product_id==item.product_id){
          state.likeadd.splice(index,1)
        }
      })
    },
    setuser(state,val){
      state.user=val
    },
    exit(state){
      state.user=""
    }
  },
  actions: {
  },
  modules: {
  },
  plugins:[new vuexPersist({
    storage:window.localStorage,
  }).plugin,]
})
